// pages/review/review.js

const app = getApp();
let tool = require('../../utils/tool.js');
const httpUtils = require('../../utils/httpUtils.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageurl: app.globalData.imageurl,
    userInfo: app.globalData.userInfo,
    goURl: '',
    isVip: true,
    flag: true,
    oLater: false,
    list: {},
    vipNums: 0,
    date: '', //学习日期
    from_channel: 1, //从学习记录进值为0   其他为1
    index: 0, //选中日期的下标
    datestrList: [], //单词学习日期列表
   
  },
  showMask: function() {
    this.setData({
      flag: false
    })
  },
  closeMask: function() {
    this.setData({
      flag: true
    })
    wx.showTabBar()
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  govip: function(e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.urldata,
    });
  },
  gotourl: function(e) {
    var that = this;
    if (this.data.list[e.currentTarget.dataset.index].must_review <= 0) {
      wx.showToast({
        title: '无需复习单词',
        icon: 'none',
        duration: 1500
      })
      return;
    } else {
      //多传个日期
      let tourl = e.currentTarget.dataset.urldata + this.data.date;
      if (this.data.isVip === false) {
        this.setData({
          flag: false, //弹出试用框
          goURl: tourl,
          vipNums: this.data.list[e.currentTarget.dataset.index].num,
          oLater: (this.data.list[e.currentTarget.dataset.index].num > 0 ? false : true)
        })
        return false
      }
      that.setData({
        from_channel: 1
      })
      wx.navigateTo({
        url: tourl,
      });
    }

  },
  
  openlater: function() {
    this.setData({
      flag: true,
      from_channel: 1
    })
    //this.setData({ oLater: true })
    wx.navigateTo({
      url: this.data.goURl,
    });
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    //获取传过来的日期
    that.setData({
      date: app.globalData.studyDate,
    })
    if (options.from_channel) {
      that.setData({
        from_channel: options.from_channel
      })
    }
   
  },

  /**
   * 获取玩法列表
   */
  getData: function() {
    let that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true
    })
    let paramArr = {
      // access_token:app.globalData.access_token
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      date: app.globalData.studyDate,
      from_channel: that.data.from_channel
    }
    let rs = tool.getSign(paramArr, app.globalData.key); //返回 bodyParam heaer sign
    let sign = rs.sign;
    let apiUrl = app.globalData.apiURL + "review/lists?sign=" + sign;
    let classPro = new Promise(
      function(resolve, reject) {
        wx.request({
          url: apiUrl,
          header: rs.header,
          data: rs.bodyParam,
          method: 'post',
          success(res) {
            resolve(res);
          }
        })
      }

    );

    classPro.then(function(res) {
      wx.hideLoading();
      if (res.data.code === 1) {
        app.globalData.studyDate =res.data.data.date;
        that.setData({
          list: res.data.data.list,
          isVip: res.data.data.list[0].vip == 1 ? true : false,
          date:res.data.data.date,
        });
      } else if (res.data.code == 10001) {
        wx.redirectTo({
          url: '/pages/login/login',
        })
      }

    })
  },

/**
 * 获取单词学习时间列表
 */
  getWordStudyTimeList:function(){
    let that = this;
    // wx.showLoading({
    //   title: '',
    //   mask: true
    // })
    httpUtils.getWordStudytimeList({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
    }).then((res) => {
      console.log(JSON.stringify(res))
      that.setData({
        datestrList: res.data
      })

      if( that.data.datestrList.length<=0 || that.data.datestrList==[] ||that.data.datestrList == null){
        wx.showModal({
          title: '提示',
          content: '暂无复习!',
          showCancel: false,
          success(res) {
            wx.switchTab({
              url: '/pages/antiforget/antiforget'
            })
          }
        })
        return;
      }

      that.getData();
    });
  },
  
  /**
   * 点击切换学习时间
   */
  bindPickerChange:function(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let that = this;
    let index = e.detail.value;
    //设置切换后的日期
    app.globalData.studyDate = that.data.datestrList[index];
    this.setData({
      index:e.detail.value,
      from_channel: 0,
      date: that.data.datestrList[index]
    })
    //重新请求获取一下玩法列表数据
    that.getData();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var that = this;
    that.setData({
      date: app.globalData.studyDate,
    })
    that.getWordStudyTimeList();
    // that.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})